package com.yzf.demo.demo.Thread;

/**
 * Created by 于占峰 on 2020/3/30/030.
 */
public class ThreadJoinDemo {

    private static int x = 0;

    private static int y = 0;

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            x = 1;
            y = 2;
        });
        Thread t2 = new Thread(() -> {
            x = y + 2;
        });
        t1.start();
        //保证t1线程的执行结果对于t2可见 使t1一定比t2优先进行
        //主线程处于阻塞状态 只有t1执行后主线程才会继续执行
        t1.join();
        t2.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Result:" + x);
    }
}
